package com.settlement.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.common.util.StringUtils;
import com.settlement.system.mapper.AuditErrorRecordMapper;
import com.settlement.system.model.entity.AuditErrorRecord;
import com.settlement.system.model.form.AuditErrorRecordForm;
import com.settlement.system.model.query.AuditErrorRecordQuery;
import com.settlement.system.model.vo.AuditErrorRecordImportVo;
import com.settlement.system.service.AuditErrorRecordService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 转换核销单失败记录表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-08-26
 */
@Service
@RequiredArgsConstructor
public class AuditErrorRecordServiceImpl extends ServiceImpl<AuditErrorRecordMapper, AuditErrorRecord> implements AuditErrorRecordService {

    private final Logger logger = LoggerFactory.getLogger(AuditErrorRecordServiceImpl.class);

    @Override
    public Page<AuditErrorRecord> getPage(AuditErrorRecordQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<AuditErrorRecord> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

    @Override
    public boolean saveAuditErrorRecord(AuditErrorRecordForm auditErrorRecordForm) {
        AuditErrorRecord auditErrorRecord = new AuditErrorRecord();
        BeanUtils.copyProperties(auditErrorRecordForm, auditErrorRecord);
        auditErrorRecord.setCreateTime(null);
        return this.saveOrUpdate(auditErrorRecord);
    }

    @Override
    public AuditErrorRecordForm getAuditErrorRecordFormData(Long id) {
        AuditErrorRecord auditErrorRecord = this.getById(id);
        AuditErrorRecordForm auditErrorRecordForm = new AuditErrorRecordForm();
        BeanUtils.copyProperties(auditErrorRecord, auditErrorRecordForm);
        return auditErrorRecordForm;
    }

    @Override
    public boolean deleteAuditErrorRecord(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
    }

    @Override
    public void importData(List<AuditErrorRecordImportVo> voList) {
        List<AuditErrorRecord> auditErrorRecordList = new ArrayList<>();
        for (AuditErrorRecordImportVo importVo : voList) {
            AuditErrorRecord auditErrorRecord = new AuditErrorRecord();
            BeanUtils.copyProperties(importVo, auditErrorRecord);
            auditErrorRecordList.add(auditErrorRecord);
        }
        this.saveOrUpdateBatch(auditErrorRecordList);
    }

    @Override
    public List<AuditErrorRecordImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<AuditErrorRecord> auditErrorRecordList = this.getBaseMapper().selectBatchIds(ids);
        List<AuditErrorRecordImportVo> voList = new ArrayList<>();
        for (AuditErrorRecord auditErrorRecord : auditErrorRecordList) {
            AuditErrorRecordImportVo vo = new AuditErrorRecordImportVo();
            BeanUtils.copyProperties(auditErrorRecord, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public void checkAndSave(AuditErrorRecord auditErrorRecord) {
        List<AuditErrorRecord> records = this.list(new LambdaQueryWrapper<AuditErrorRecord>().eq(AuditErrorRecord::getDataType, auditErrorRecord.getDataType()).eq(AuditErrorRecord::getDataId, auditErrorRecord.getDataId()));
        if (records.size() > 0) {
            // 更新状态
            for (AuditErrorRecord record : records) {
                logger.info("更新一条核销单转换失败记录：{}", record.toString());
                BeanUtils.copyProperties(auditErrorRecord, record, "id");
                record.setUpdateTime(LocalDateTime.now());
                record.setStatus(auditErrorRecord.getStatus());
                record.setRemark(auditErrorRecord.getRemark());
                record.setUpdateTime(LocalDateTime.now());
                record.setTryNumber(record.getTryNumber() + 1);
                record.cutString();
            }
            this.updateBatchById(records);
        } else {
            logger.info("新增一条核销单转换失败记录：{}", auditErrorRecord);
            auditErrorRecord.cutString();
            this.save(auditErrorRecord);
        }
    }
}
